// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Globalization;
using System.Web.UI.WebControls;
using BlueOnionSoftware.Bloget.Properties;

namespace BlueOnionSoftware.Bloget
{
    internal class IndexView : View
    {
        internal IndexView(Bloget blogetArg)
            : base(blogetArg)
        {
        }

        protected override string RootClassId()
        {
            return "index-view";
        }

        protected override void ShowView()
        {
            Index(false, Bloget.QueryCategory);
        }

        protected void Index(bool draftsOnly, Category category)
        {
            BlogView.ShowHeader(Bloget);

            if (Bloget.IndexHeaderTemplate != null)
            {
                var blogContainer = new BlogContainerControl(Bloget, Bloget.Blog);
                Bloget.IndexHeaderTemplate.InstantiateIn(blogContainer);
                Bloget.Controls.Add(blogContainer);
            }

            var year = -1;
            var month = -1;
            var noEntries = true;

            foreach (var post in Bloget.Blog.EnumeratePosts(0, Bloget.Blog.Posts.Count, category, draftsOnly, false))
            {
                if (post.Created.Year != year)
                {
                    IndexYearSeparator(post);
                    year = post.Created.Year;
                    month = -1;
                }

                if (post.Created.Month != month)
                {
                    IndexMonthSeparator(post, (month == -1));
                    month = post.Created.Month;
                }

                IndexEntry(post);
                noEntries = false;
            }

            if (noEntries)
            {
                Bloget.WriteLine("<p>" + Resources.IndexViewNoEntriesFound + "</p>");
            }

            if (Bloget.IndexFooterTemplate != null)
            {
                var blogContainer = new BlogContainerControl(Bloget, Bloget.Blog);
                Bloget.IndexFooterTemplate.InstantiateIn(blogContainer);
                Bloget.Controls.Add(blogContainer);
            }

            BlogView.ShowFooter(Bloget);
            Bloget.PoweredByBloget();
        }

        private void IndexEntry(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            if (Bloget.IndexTemplate != null)
            {
                var indexContainer = new IndexContainerControl(Bloget, Bloget.Blog, post);
                Bloget.IndexTemplate.InstantiateIn(indexContainer);
                Bloget.Controls.Add(indexContainer);
            }

            else
            {
                Bloget.WriteLine(post.Created.ToString("MM-dd-yyyy", CultureInfo.CurrentCulture) + "&nbsp;&nbsp;");
                var link = new HyperLink
                {
                    Text = post.Title,
                    NavigateUrl = Bloget.BuildQueryString(Mode.Post, post, 0, null, null, DateTime.MinValue, null)
                };
                Bloget.Controls.Add(link);
                Bloget.WriteLine("<br/>");
            }
        }

        private void IndexMonthSeparator(Post post, bool firstMonth)
        {
            if (Bloget.IndexMonthSeparatorTemplate != null)
            {
                var indexContainer = new IndexContainerControl(Bloget, Bloget.Blog, post);
                Bloget.IndexMonthSeparatorTemplate.InstantiateIn(indexContainer);
                Bloget.Controls.Add(indexContainer);
            }

            else
            {
                if (firstMonth == false)
                {
                    Bloget.WriteLine("<br/>");
                }
            }
        }

        private void IndexYearSeparator(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            if (Bloget.IndexYearSeparatorTemplate != null)
            {
                var indexContainer = new IndexContainerControl(Bloget, Bloget.Blog, post);
                Bloget.IndexYearSeparatorTemplate.InstantiateIn(indexContainer);
                Bloget.Controls.Add(indexContainer);
            }

            else
            {
                Bloget.WriteLine(@"<h3 class=""index-year"">" +
                    post.Created.Year.ToString(CultureInfo.CurrentCulture) + "</h3>");
            }
        }
    }
}